$(function () {


    $("#forgotPwd").click(function () {
        window.location.href = "forget";
    });

    $("#signUp").click(function () {
        window.location.href = 'register'
    });


    $("#page_btn").click(function () {
        window.history.go(-1);
    })

    // 重置密码发送验证码
    $("#forget_opt_btn").click(function () {
        var mobileNumber = $("#forget_mobile_number").val();
        if (mobileNumber === '' || mobileNumber.trim().length === 0) {
            toast('Phone Number is required');
            return;
        }
        mobileNumber = '91' + mobileNumber;
        if (
            /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[6789]\d{9}$/.test(
                mobileNumber
            )
        ) {
            console.log("objk")
        } else {
            toast('Invalid Phone Number');
            return;
        }

        // 调用发送验证码接口
        $.ajax({
            type: "POST",
            url: "../login/sendPhoneCode",
            data: "mobileNumber=" + mobileNumber + "&type=1",
            success: function (msg) {
                var msg = msg.data.msg;
                if (msg === 'OK') {
                    toast('The verification code has been sent');
                    var t = 59;
                    var timer = setInterval(function () {
                        if (t == 0) {
                            $("#forget_opt_btn").text("OTP");
                            clearInterval(timer)
                        } else {
                            $("#forget_opt_btn").text(t);
                            t--;
                        }
                    }, 1000)
                } else {
                    toast('Failed to send the verification code');
                }
            }
        })
    })

    // 忘记密码  重置密码
    $("#resetPwd").click(function () {
        var forget_verification_code = $("#forget_verification_code").val();
        var forget_password = $("#forget_password").val();
        var forget_mobile_number = $("#forget_mobile_number").val();

        if (forget_mobile_number === '' || forget_mobile_number.trim().length === 0) {
            toast('Mobile Number is required');
            return;
        }

        if (forget_verification_code === '' || forget_verification_code.trim().length === 0) {
            toast('Verification Code is required');
            return;
        }
        if (forget_password === '' || forget_password.trim().length === 0) {
            toast('password is required');
            return;
        }

        // 获取验证码
        $.ajax({
            type: "GET",
            url: "../login/isEfficacious",
            data: "mobileNumber=" + forget_mobile_number + "&phoneCode=" + forget_verification_code,
            success: function (msg) {
                var flag = msg.data;
                if (flag) {
                    resetPwd(forget_mobile_number, forget_password);
                } else {
                    toast('The verification code does not match');
                    return;
                }
            }
        })
    });


    function resetPwd(mobileNumber, password) {
        $.ajax({
            type: "POST",
            url: "../login/resetPwd",
            data: "mobileNumber=" + mobileNumber + "&pwd=" + password,
            success: function (msg) {
                var flag = msg.data;
                if (flag) {
                    toast('Password reset succeeded');
                    window.location.href = 'login';
                }
            }
        })
    }

    // 注册发送验证码
    $("#register_otp_btn").click(function () {
        var mobileNumber = $("#register_mobile_number").val();
        if (mobileNumber === '' || mobileNumber.trim().length === 0) {
            toast('Phone Number is required');
            return;
        }
        mobileNumber = '91' + mobileNumber;
        if (
            /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[6789]\d{9}$/.test(
                mobileNumber
            )
        ) {
            console.log("objk")
        } else {
            toast('Invalid Phone Number');
            return;
        }

        // 调用发送验证码接口
        $.ajax({
            type: "POST",
            url: "../login/sendPhoneCode",
            data: "mobileNumber=" + mobileNumber + "&type=0",
            success: function (msg) {
                var msg = msg.data.msg;
                if (msg === 'OK') {
                    toast('The verification code has been sent');
                    var t = 59;
                    var timer = setInterval(function () {
                        if (t == 0) {
                            $("#register_otp_btn").text("OTP");
                            $("#register_otp_btn").removeAttr("disabled");
                            clearInterval(timer)
                        } else {
                            $("#register_otp_btn").attr("disabled","disabled");
                            $("#register_otp_btn").text(t);
                            t--;
                        }
                    }, 1000)
                } else {
                    toast('Failed to send the verification code');
                }
            }
        })
    })
    // 登录
    $("#login_btn").click(function () {
        var loginName = $("#login_mobile_number").val();
        loginName = '91' + loginName;
        var password = $("#login_password").val();
        if (
            /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[6789]\d{9}$/.test(
                loginName
            )
        ) {
            console.log("objk")
        } else {
            toast('Invalid Phone Number');
            return;
        }

        if (password === '' || password.trim().length === 0) {
            toast('password is required');
            return;
        }
        console.log(loginName)
        console.log(password)
        // var turnForm = document.createElement("form");
        // //一定要加入到body中！！
        // document.body.appendChild(turnForm);
        // turnForm.method = 'post';
        // turnForm.action = '/login';
        // //创建隐藏表单
        // var userNameElement = document.createElement("input");
        // userNameElement.setAttribute("name", "username");
        // userNameElement.setAttribute("type", "hidden");
        // userNameElement.setAttribute("value", loginName);
        // var passwordElement = document.createElement("input");
        // passwordElement.setAttribute("name", "password");
        // passwordElement.setAttribute("type", "hidden");
        // passwordElement.setAttribute("value", password);
        // turnForm.appendChild(userNameElement);
        // turnForm.appendChild(passwordElement);
        // turnForm.submit();
        $.ajax({
            url: "/login",
            data: {
                "username": loginName,
                "password": password
            },
            dataType: "json",
            type: 'post',
            async: false,
            success: function (data) {
                console.log('后端返回来的data的值：', data);
                if (!data.success) {
                    toast(data.msg);
                } else {
                    window.location.href = "/mime";
                }
            }
        })
    })

    $("#registerLogin").click(function () {
        window.location.href = "login"
    })

    // 创建账号
    $("#create_account_btn").click(function () {
        var mobile_number = $("#register_mobile_number").val();
        mobile_number = '91' + mobile_number;
        var verification_code = $("#register_verification_code").val();
        var register_password = $("#register_password").val();
        var rcode = $("#rcode").val();
        var data = {
            loginName: mobile_number,
            mobileCode: verification_code,
            password: register_password,
            rcode: rcode
        }
        // 注册用户
        $.ajax({
            type: "POST",
            url: "../registerUser",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (msg) {
                var success = msg.success;
                if (!success) {
                    var data = msg.data;
                    toast(data);
                } else {
                    toast('registration successful');
                    $("#register_mobile_number").val("");
                    $("#register_verification_code").val("");
                    $("#register_password").val("");
                    $("#register_otp_btn").text("OTP");
                    $("#register_otp_btn").removeAttr("disabled");
                    setTimeout(function () {
                        window.location.href = "login";
                    }, 1000);

                }
            }
        })
    })


    // 注销
    $("#logOutBtn").click(function () {
        confirm({
            title: 'Confirm',
            content: 'Do you want to logout?',
            doneText: 'YES',
            cancelText: 'NO'
        }).then(() => {
            window.location.href = "logout";
        }).catch(() => {
            console.log('已取消')
        })
    })
})